package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class NewsFragment extends Fragment {

    private RecyclerView recyclerView;
    private ArticleAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news, container, false);

        recyclerView = view.findViewById(R.id.rv_articles);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        // 模拟数据
        List<Article> articles = new ArrayList<>();
        articles.add(new Article("Android开发指南",
                "本文详细介绍了Android开发的基本流程和最佳实践...",
                "谷歌开发者", "2023-09-20"));
        articles.add(new Article("Kotlin入门教程",
                "学习Kotlin语言的基本语法和特性...",
                "JetBrains团队", "2023-09-19"));

        adapter = new ArticleAdapter(articles);
        recyclerView.setAdapter(adapter);

        return view;
    }

    // Adapter类
    private class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ViewHolder> {
        private final List<Article> articles;

        public ArticleAdapter(List<Article> articles) {
            this.articles = articles;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_article, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Article article = articles.get(position);
            holder.tvTitle.setText(article.getTitle());
            holder.tvContent.setText(article.getContent());

            holder.itemView.setOnClickListener(v -> {
                Intent intent = new Intent(getActivity(), ArticleDetailActivity.class);
                intent.putExtra("title", article.getTitle());
                intent.putExtra("content", article.getContent());
                intent.putExtra("author", article.getAuthor());
                intent.putExtra("time", article.getTime());
                startActivity(intent);
            });
        }

        @Override
        public int getItemCount() {
            return articles.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tvTitle, tvContent;

            ViewHolder(View itemView) {
                super(itemView);
                tvTitle = itemView.findViewById(R.id.tv_title);
                tvContent = itemView.findViewById(R.id.tv_content);
            }
        }
    }
}